package com.base.db.mysql

import com.base.model.api.UserModel_Admin

//获取用户_用户名密码方式
fun MySQLService.admin_getUserByAccount(loginname: String, password: String): String? {
    if (loginname.isBlank() || password.isBlank()) return null
    return mapper.admin_getUserByAccount(loginname, password)?.firstOrNull()?.get("id")?.toString()
}

//获取用户登录信息
fun MySQLService.admin_getUserInfoByUID(uid: String): UserModel_Admin? {
    if (uid.isBlank()) return null
    return mapper.admin_getUserInfoByUID(uid)?.firstOrNull()
}

//通过uid获取角色
fun MySQLService.admin_getRoleJkidsByUid(uid: String?): String? {
    if (uid.isNullOrBlank()) return null
    return mapper.admin_getJkidsByUid(uid).firstOrNull()?.get("jkids")?.toString()
}

//查询商品总数
fun MySQLService.admin_getSPUTotal(searchContent: String?): Int {
    return mapper.admin_getSPUTotal(searchContent)
}

//关键字+分页，查询商品
fun MySQLService.admin_getSPUs(page: Int, pageSize: Int, searchContent: String?): List<Map<String, Any>> {
    if (page <= 0 || pageSize <= 0) return listOf()
    return mapper.admin_getSPUs((page - 1) * pageSize, pageSize, searchContent) ?: listOf()
}

//新增SPU
fun MySQLService.admin_addSPU(
        title: String,
        img_main: String,
        video: String?,
        img_banner: String?,
        img_detail: String?,
        des: String?,
        content_like: String?,
        params_pinpai: String?,
        params_xinghao: String?,
        params_tuihuan: String,
): Boolean {
    if (title.isBlank() || img_main.isBlank()) return false
    return mapper.admin_addSPU(title, img_main, video, img_banner, img_detail, des, content_like, params_pinpai, params_xinghao, params_tuihuan) > 0
}

//编辑SPU
fun MySQLService.admin_updateSPU(
        id: String,
        title: String,
        des: String?,
        content_like: String?,
        params_pinpai: String?,
        params_xinghao: String?,
        params_tuihuan: String,
): Boolean {
    if (id.isBlank() || title.isBlank()) return false
    return mapper.admin_updateSPU(id, title, des, content_like, params_pinpai, params_xinghao, params_tuihuan) > 0
}

//删除SPU
fun MySQLService.admin_deleteSPU(id: String): Boolean {
    if (id.isBlank()) return false
    return mapper.admin_deleteSPU(id) > 0
}